<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of signinModel
 *
 * @author xlp
 */
class signinModel extends model {

    protected $today = null;
    protected $yesterday = null;
    protected $exchangeCredit = 100; //一棵树兑换的积分数
    protected $cycle = 7; //种植周期

    function __construct() {
        parent::__construct();
        $this->dbTable = 'user_signin';
        $this->today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $this->yesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
    }

    /*
     * 获取签到数据
     */

    function getSigninInfoByUid($uid) {
        $rs = $this->where(array('uid' => $uid))->find();
        $rs['isSignin'] = false;
        $rs['leaveTimes'] = $this->cycle; //浇水剩余天数
        $rs['hasTimes'] = 0; //已浇水天数
        if (isset($rs['uid'])) {
            if ($rs['signin_time'] == $this->today) {//今日是否签到
                $rs['isSignin'] = true;
            }
            if ($rs['counts'] && ($rs['signin_time'] == $this->yesterday || $rs['signin_time'] == $this->today)) {//表示是连续签到
                $rs['hasTimes'] = $rs['counts'] % $this->cycle;
                $rs['leaveTimes'] = $this->cycle - $rs['hasTimes'];
            } else {
                $rs['counts'] = 0;
            }
        } else {
            $rs['is_harvest'] = 0;
            $rs['counts'] = 0;
        }
        return $rs;
    }

    /*
     * 签到
     */

    function setSignin($uid = '') {
        $signinTimes = 1; //连续签到次数
        $isRipe = false; //是否种满7天可以收割
        $update = ''; //更新字段内容
        $rs = $this->getSigninInfoByUid(array('uid' => $uid));
        if (isset($rs['uid'])) {
            if ($rs['signin_time'] == $this->yesterday) {//判断最后签到时间是不是昨天
                if ($rs['counts'] && !(($rs['counts'] + 1) % $this->cycle)) {//为0时说明已浇满7天
                    $isRipe = true;
                    $update .= 'is_harvest=1,';
                }
                $update .= 'counts=counts+1';
                $signinTimes = $rs['counts'] + 1;
            } else {//不是连续签到，则还原连签次数
                $update = 'counts=1';
                $signinTimes = 1;
            }
//            $this->today = $this->yesterday;
            $this->update('signin_time=' . $this->today . ' , ' . $update, array('uid' => $uid));
        } else {
            $this->insert(array('uid' => $uid, 'signin_time' => $this->today, 'counts' => 1, 'now_breed' => 0, 'myplants' => 0, 'is_harvest' => 0));
        }
        $credit = $this->getCredits($signinTimes);
        //增加积分和日志
        D('member')->setUserScore(array('uid' => $uid, 'act' => 1, 'credit' => $credit, 'event' => 1, 'fid' => 0, 'content' => '签到'), 'event');
        return array('signinTimes' => $signinTimes, 'credits' => $credit, 'next' => $this->getCredits(($signinTimes + 1)), 'isRipe' => $isRipe);
    }

    /*
     * 种树
     */

    function setPlant($uid, $plantId) {
        $rs = $this->getSigninInfoByUid(array('uid' => $uid));
        if (isset($rs['uid'])) {
            $id = $this->update(array('now_breed' => $plantId, 'is_harvest' => 0), array('uid' => $uid));
        } else {
            $id = $this->insert(array('uid' => $uid, 'signin_time' => 0, 'counts' => 0, 'now_breed' => $plantId, 'myplants' => 0, 'is_harvest' => 0));
        }
        return $id;
    }

    /*
     * 收割
     */

    function setHarvest($uid) {
        $rs = $this->getSigninInfoByUid(array('uid' => $uid));
        if (isset($rs['is_harvest']) && $rs['is_harvest']) {//为0时说明已浇满7天
            $this->update('is_harvest=0,now_breed=0,myplants=myplants+1', array('uid' => $uid));
            return $rs['myplants'] + 1;
        }
        return 0;
    }

    /*
     * 兑换积分
     */

    function exchange($uid, $count = 1) {
        $credit = 0;
        $rs = $this->getSigninInfoByUid(array('uid' => $uid));
        if ($rs && $rs['myplants']) {
            if ($rs['myplants'] < $count) {
                return 0;
            }
            $this->where(array('uid' => $uid))->setDec('myplants', $count);
            //增加积分和日志
            $credit = $this->exchangeCredit * $count;
            D('member')->setUserScore(array('uid' => $uid, 'act' => 1, 'credit' => $credit, 'event' => 1, 'fid' => 0, 'content' => '签到兑换积分'), 'event');
            return array('credit' => $credit, 'has' => $rs['myplants'] - $count);
        }
        return 0;
    }

    /*
     * 获取签到获得积分数
     */

    private function getCredits($signinTimes) {
        if ($signinTimes < 4) {
            $credit = $signinTimes * 10;
        } else {
            $credit = 30;
        }
        return $credit;
    }

}
